<template>
  <el-dialog :title="saveObj.title" :visible.sync="saveObj.show">
    <el-form
      ref="form"
      :rules="rules"
      :model="formData"
      label-width="108px"
      size="small"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="企业ID" prop="enterpriseUid">
            <el-input v-model="formData.enterpriseUid" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="水厂编码" prop="worksCode">
            <el-input v-model="formData.worksCode" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="水厂名称" prop="worksName">
            <el-input v-model="formData.worksName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="水质标准" prop="stdUid">
            <el-select v-model="formData.stdUid" placeholder="请选择水质标准">
              <el-option
                v-for="item in waterOpts"
                :key="item.stdUid"
                :label="item.stdName"
                :value="item.stdUid"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="水厂地址" prop="worksAddress">
            <el-input v-model="formData.worksAddress" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="水厂经度" prop="worksLng">
            <el-input v-model="formData.worksLng" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="水厂纬度" prop="worksLat">
            <el-input v-model="formData.worksLat" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="水厂厂长" prop="userName">
            <el-select v-model="formData.userName" placeholder="请选择">
              <el-option
                v-for="item in userOpts"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="工艺主管" prop="craftUserName">
            <el-select v-model="formData.craftUserName" placeholder="请选择">
              <el-option
                v-for="item in userOpts"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备主管" prop="equUserName">
            <el-select v-model="formData.equUserName" placeholder="请选择">
              <el-option
                v-for="item in userOpts"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="水厂排序" prop="sort">
            <el-input v-model.number="formData.sort" type="number" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="水厂描述">
            <el-input v-model="formData.worksDesc" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFn('form')">取 消</el-button>
      <el-button type="primary" @click="sumbitFn('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getSysStandardList } from '@/api/infoConfig/waterStandard'
import {
  addsysWork,
  getUserList,
  checkSysWorks
} from '@/api/infoConfig/factoryManage'
export default {
  props: {
    saveObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      // 水质标准
      waterOpts: [],
      // 水厂厂长
      userOpts: [],
      isEdit: true,
      rules: {
        enterpriseUid: [
          { required: true, message: '此为必填项', trigger: 'blur' }
        ],
        worksCode: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        worksName: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        stdUid: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        userName: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        craftUserName: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        equUserName: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        sort: [{ type: 'number', message: '排序必须为数字值' }]
      }
    }
  },
  mounted() {},
  methods: {
    checkEdit(data) {
      this.isEdit = data
    },
    async getFormFn() {
      this.formData = this.saveObj.data
      await this.getSysStandardListFn()
      await this.getUserListFn()
      this.saveObj.show = true
    },
    // 获取水质标准列表
    getSysStandardListFn() {
      return getSysStandardList({ name: '' }).then(res => {
        // console.log(res)
        this.waterOpts = res.data
      })
    },
    // 获取水厂厂长
    async getUserListFn() {
      return getUserList().then(res => {
        console.log(res)
        this.userOpts = res.rows
      })
    },
    // 校验水厂编码
    checkSysWorksFn(code) {
      checkSysWorks({ name: code }).then(res => {
        // console.log(res)
        if (res.data === 0) {
          this.addsysWorkFn()
        }
      })
    },
    // 添加水厂
    addsysWorkFn() {
      addsysWork(this.formData).then(res => {
        // console.log(res)
        this.$emit('getWorks')
        this.closeFn('form')
      })
    },
    closeFn(data) {
      this.saveObj.show = false
      this.$refs[data].resetFields()
    },
    sumbitFn(data) {
      this.$refs[data].validate(valid => {
        // 表单验证
        if (valid) {
          // console.log(this.formData)
          if (this.isEdit) {
            this.addsysWorkFn()
          } else {
            this.checkSysWorksFn(this.formData.worksCode)
          }
          // this.addsysWorkFn()
        } else {
          this.$message.error('*为必填项')
        }
      })
    }
  }
}
</script>
